package com.open.party.cloud.view.base

import android.os.Bundle
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppEventBusRecycleViewTitleBaseActivity
import com.sinothk.android.utils.inters.OnSuperListener
import com.open.party.cloud.R
import com.open.party.cloud.model.TownshipBean
import com.open.party.cloud.model.PartyOrgVo
import com.open.party.cloud.model.data.CodeValue
import com.open.party.cloud.view.home.jiCengDangJian.org.adapter.OrgForCountryListAdapter
import com.open.party.cloud.viewModel.BizViewModel
import com.sinothk.rxretrofit.bean.PageVo
import kotlinx.android.synthetic.main.comm_activity_load_recycle_view_load_tip_app_bg.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class TownshipUnitSelectActivity : AppEventBusRecycleViewTitleBaseActivity<PartyOrgVo>(), OnSuperListener<TownshipBean> {

    var viewModel: BizViewModel? = null
    var orgName = "所属区域"

    private var adapter: OrgForCountryListAdapter? = null

    override fun getLayoutResId(): Int = R.layout.comm_activity_load_recycle_view_load_tip_app_bg

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewTitle(orgName)

        viewModel = BizViewModel()

        initView()
    }

    private fun initView() {
        initRecycleLinearView(loadingRecyclerView)

        adapter = OrgForCountryListAdapter(this)
        loadingRecyclerView.adapter = adapter
        adapter?.setOnSuperListener(this)
        loadingRecyclerView.setLoadingMoreEnabled(false)

        loadingTipView.setRetryListener {
            refresh()
        }
        refresh()
    }

    private fun refresh() {
        loadingTipView.showLoading("努力加载中")
        refreshData()
    }

    override fun loadData(pageVo: PageVo<PartyOrgVo>) {
        viewModel!!.getCommunityList("")
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<ArrayList<TownshipBean>>) {
        if (result.eventType != "getCommunityList") {
            return
        }

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data.size == 0) {
                    loadingTipView.showEmpty("暂无数据", R.drawable.empty_nodata)
                    loadingRecyclerView.setLoadingMoreEnabled(false)
                } else {
                    adapter!!.setData(result.data!!)
                    loadingTipView.showContent()
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                loadingTipView.showError("Token过期，请重新登录")
            }
            else -> {
                loadingTipView.showError(result.msg)
            }
        }

        stopLoading(loadingRecyclerView, loadType)
    }

    override fun onClick(position: Int, itemData: TownshipBean?, flag: String?) {
        if (itemData != null) {
            intent.putExtra(CodeValue.Data, itemData)
            setResult(CodeValue.ResultCodeSuccess, intent)
            finish()
        }
    }
}